package com.rr.learningdemo.newfeature;

import com.rr.learningdemo.pojo.Person;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author RR
 * @date 2021/3/16 17:40
 */
public class StreamTest {

    public static void main(String[] args) {
        List<Person> list = new ArrayList<>();
        list.add(new Person("name1",1));
        list.add(new Person("name2",2));
        list.add(new Person("name3",3));
        list.add(new Person("name4",4));

        //过滤，映射
        List<String> names = list.stream()
                .filter(p -> p.getAge() > 2)
                .map(Person::getName)
                .collect(Collectors.toList());
        System.out.println(names.toString());
        //转map
        Map<String, Integer> map = list.stream().collect(Collectors.toMap(Person::getName, Person::getAge));
        System.out.println(map.toString());
        //排序
        List<Person> personAfterSort = list.stream().sorted((p1, p2) -> p1.getAge() < p2.getAge() ? 0 : -1).collect(Collectors.toList());
        System.out.println(personAfterSort.toString());
    }
}
